﻿@model Von.Web.Models.ForgotPasswordViewModel
@{
    ViewData["Title"] = "密码找回";
}

<h2>@ViewData["Title"]</h2>
<h4>录入您要找回的邮箱或手机号码，您可以通过邮件或短信重置用户密码。</h4>
<hr />
@Html.Partial("~/Views/_StatusMessage.cshtml", Model?.StatusMessage)
<div class="row">
    <div class="col-md-6">
        <form asp-action="ForgotPassword" method="post">
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="input-group">
                <label asp-for="FromEmail" class="input-group-addon"></label>
                <div class="form-control">
                    <label class="radio-inline">
                        <input type="radio" name="FromEmail" id="FromEmail1" value="0" checked>通过邮箱找回
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="FromEmail" id="FromEmail2" value="1">通过手机找回
                    </label>
                </div>
            </div>
            <br />
            <div class="input-group">
                <label asp-for="LoginValue" class="input-group-addon"></label>
                <input asp-for="LoginValue" class="form-control" />
                <span asp-validation-for="LoginValue" class="text-danger"></span>
                <input type="hidden" id="SendValue" />
                <span class="input-group-btn">
                    <a id="msgSender" class="btn btn-default" onclick="sendMsg()">发送请求</a>
                </span>
            </div>
            <br />
            <label id="SendResult" class="text-info"></label>
            <br />
            <div class="input-group">
                <label asp-for="RedeemValue" class="input-group-addon"></label>
                <input id="RedeemValue" asp-for="RedeemValue" class="form-control" />
                <span asp-validation-for="RedeemValue" class="text-danger"></span>
                <input type="hidden" id="FindKey" />
                <span class="input-group-btn">
                    <a class="btn btn-default" onclick="findPassword()">找回密码</a>
                </span>
            </div>
        </form>
    </div>
</div>
<div class="modal fade" id="passwordDialog" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="ModalLabel">更改密码</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <span>更新口令</span>
                    <input id="InputPassword" class="form-control" type="password" onchange="inputPassword()" />
                    <span id="CheckPassword" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <span>验证口令</span>
                    <input class="form-control" type="password" id="ConfirmPassword" onchange="checkPassword()" />
                    <span id="CheckConfirm" class="text-danger"></span>
                </div>
            </div>
            <div class="modal-footer">
                <div class="input-group">
                    <span class="input-group-btn">
                        <a class="btn btn-default" onclick="changePassword()">重新设置</a>
                    </span>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var cntToWait = 60;
    var timer;
    function waitSend() {
        $("#msgSender").attr("disabled", true); $("#msgSender").text("发送请求(" + cntToWait + ")"); cntToWait--;
        if (cntToWait <= 0) { clearInterval(timer); cntToWait = 60; $("#msgSender").removeAttr("disabled"); }
    }
    function sendMsg() {
        var arr = [];
        for (i = 0; i < 24; i++) { arr.push(parseInt(Math.random() * 256)); }
        $("#SendValue").val(arr);
        var data = {
            "FromEmail": $("#FromEmail1").attr("checked") == "checked" ? true : false,
            "LoginValue": $("#LoginValue").val(),
            "SendValue": arr
        }
        timer = setInterval(function () { waitSend() }, 1000);
        AjaxPostData('@Url.Ajax("SendMsgToLogin")', data, function (result) {
            $("#sendMsg").val("申请已发送，请及时收信息，录入验证码，点击找回密码即可。");
            $("#FindKey").val(result);
        });
    }
    function findPassword() {
        var data = {
            "FromEmail": $("#FromEmail1").attr("checked") == "checked" ? true : false,
            "LoginValue": $("#LoginValue").val(),
            "SendValue": $("#SendValue").val(),
            "FindKey": $("#FindKey").val(),
            "RedeemValue": $("#RedeemValue").val()
        }
        AjaxPostData('@Url.Ajax("ReciveMsgToLogin")', data, function (result) {
            if (result) {
                $("#passwordDialog").modal('show');
            }
        });
    }
    function inputPassword() {
        var regex = new RegExp("@ViewBag.PWDRegex.Replace("\\", "\\\\")", "g");
        if (!regex.test($('#InputPassword').val())) $("#CheckPassword").text("@ViewBag.PWDRequirement");
        else $("#CheckPassword").text("");
    }
    function checkPassword() {
        if ($('#InputPassword').val() != $('#ConfirmPassword').val()) $("#CheckConfirm").text("密码录入不一致！");
        else $("#CheckConfirm").text("");
    }
    function changePassword() {
        inputPassword(); checkPassword(); if ($("#CheckPassword").text() != "") return; if ($("#CheckConfirm").text() != "") return;
        var data = { "Password": $("#InputPassword").val() }
        AjaxPostData('@Url.Ajax("ChangePassword")', data, function (result) { if (result) $('#passwordDialog').modal('hide'); });
    }
</script>
@section Scripts {
    @await Html.PartialAsync("_ValidationScriptsPartial")
}
